﻿using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using TabbedTemplate.ViewModels;

namespace UnitTest.ViewModels
{
    public class TestStorePunchesViewModel
    {
        [Test]
        public async Task TestDeleteHistoryCommandFunction()
        {
            var a = new PunchTimeStampParameter
            {
                Store = new Store(),
                PunchInfo = new PunchInfo(),
                DateTime = new DateTime(),
                Scores = new ConfidenceScores()
            };
            var punchinfoStorageMock = new Mock<IPunchInfoStorage>();
            var punchinfoStorage = punchinfoStorageMock.Object;
            var storePunchesViewModel =
                new StorePunchesViewModel(null, null, null, punchinfoStorage, null);
            await storePunchesViewModel.DeleteHistoryCommandFunction(a);
            punchinfoStorageMock.Verify(p => p.DeletePunchInfoByIdAsync(a.PunchInfo), Times.Once);
        }

        [Test]
        public async Task TestFavoriteToggledCommandFunction()
        {
            var favoriteStorageMock = new Mock<IFavoriteStorage>();
            var favoriteStorage = favoriteStorageMock.Object;
            var storeStorageMock = new Mock<IStoreStorage>();
            var storeStorage = storeStorageMock.Object;
            var storePunchesViewModel =
                new StorePunchesViewModel(null, favoriteStorage, storeStorage, null, null);
            var a = new Store();
            var b = new Favorite
            {
                IsFavorite = false,
                StoreUid = "",
                Timestamp = 000
            };
            storePunchesViewModel.Store = a;
            storePunchesViewModel.Favorite = b;
            storePunchesViewModel.isFavorite = true;
            await storePunchesViewModel.FavoriteToggledCommandFunction();
            favoriteStorageMock.Verify(p => p.SaveFavoriteAsync(b, true), Times.Once);
            storeStorageMock.Verify(p => p.InsertAStore(a), Times.Once);

        }

        [Test]
        public async Task TestPageAppearingCommand()
        {
            var favoriteStorageMock = new Mock<IFavoriteStorage>();
            var favoriteStorage = favoriteStorageMock.Object;
            var punchinfoStorageMock = new Mock<IPunchInfoStorage>();
            var punchinfoStorage = punchinfoStorageMock.Object;
            var preferenceStorageMock = new Mock<IPreferenceStorage>();
            var preferenceStorage = preferenceStorageMock.Object;
            var storePunchesViewModel =
                new StorePunchesViewModel(null, favoriteStorage, null, punchinfoStorage, preferenceStorage);
            var a = new Store();
            storePunchesViewModel.Store = a;
            var tempCollection = new List<PunchInfo>();
            var punchinfo1 = new PunchInfo
            {
                Store_Uid = "",
                Content = "",
                Id = 1,
                Timestamp = 1
            };
            tempCollection.Add(punchinfo1);
            punchinfoStorageMock.Setup(p => p.GetAllPunchInfoByStoreUid(a.Uid)).ReturnsAsync(tempCollection);
            await storePunchesViewModel.PageAppearingCommandFunction();
            Assert.AreEqual(storePunchesViewModel.PunchHistoryCollection[0].PunchInfo, punchinfo1);
        }

        [Test]
        public async Task TestPunchTappedCommandFunction()
        {
            var preferenceStorageMock = new Mock<IPreferenceStorage>();
            var mockPreferenceStorage = preferenceStorageMock.Object;
            var punchNavigationServiceMock =
                new Mock<IPunchNavigationService>();
            var mockPunchNavigationService = punchNavigationServiceMock.Object;
            var store = new Store { Uid = "123" };
            var storePunchPageViewModel = new StorePunchesViewModel(
                mockPunchNavigationService, null, null, null,
                mockPreferenceStorage);
            storePunchPageViewModel.Store = store;
            await storePunchPageViewModel.PunchTappedCommandFunction();
        }

        [Test]
        public async Task TestPunchMessageTappedCommandFunction()
        {
            var param = new PunchTimeStampParameter();
            var preferenceStorageMOck = new Mock<IPreferenceStorage>();
            var mockPreferenceStorage = preferenceStorageMOck.Object;
            var punchNavigationServiceMock =
                new Mock<IPunchNavigationService>();
            var mockPunchNavigationService = punchNavigationServiceMock.Object;
            var store = new Store { Uid = "123" };
            var storePunchPageViewModel = new StorePunchesViewModel(
                mockPunchNavigationService, null, null, null,
                mockPreferenceStorage);
            storePunchPageViewModel.Store = store;
            await storePunchPageViewModel.PunchMessageTappedCommandFunction(param);
            preferenceStorageMOck.Verify(p => p.Set("PunchEdit", true), Times.Once);
            punchNavigationServiceMock.Verify(p => p.NavigateToPunchViewPage(param), Times.Once);
        }
    }
}
